package GUI.Productos;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.IngredientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SelectIngredients extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Table table_1;
	IngredientVO[] i;
	ArticleVO arts;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SelectIngredients(Shell parent, int style, IngredientVO[] ings, ArticleVO ar) {
		super(parent, style);
		setText("SWT Dialog");
		i=ings;
		arts=ar;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		final IngredientVO [] ings = uso.getAllIngredients();
		System.out.println("numero de ingredientes" + ings.length);
		shell = new Shell(getParent(), getStyle());
		shell.setSize(582, 369);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.right = new FormAttachment(0, 202);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(188);
		tblclmnNewColumn.setText("New Column");
		
		Button agregar = new Button(shell, SWT.NONE);
		agregar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] item = table.getSelection();
				TableItem t = new TableItem(table_1, SWT.NONE);
				t.setText(0, item[0].getText(0));
				
				table.remove(table.getSelectionIndex());
			}
		});
		FormData fd_agregar = new FormData();
		agregar.setLayoutData(fd_agregar);
		agregar.setText("Agregar a la Lista");
		
		Button quitar = new Button(shell, SWT.NONE);
		quitar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] item = table_1.getSelection();
				TableItem t = new TableItem(table, SWT.NONE);
				t.setText(0, item[0].getText(0));
				
				table_1.remove(table_1.getSelectionIndex());
			}
		});
		
		fd_agregar.bottom = new FormAttachment(quitar, -46);
		fd_agregar.right = new FormAttachment(quitar, 0, SWT.RIGHT);
		FormData fd_quitar = new FormData();
		fd_quitar.left = new FormAttachment(table, 19);
		fd_quitar.top = new FormAttachment(0, 125);
		quitar.setLayoutData(fd_quitar);
		quitar.setText("Quitar de la lista");
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		fd_table.bottom = new FormAttachment(table_1, 0, SWT.BOTTOM);
		FormData fd_table_1 = new FormData();
		fd_table_1.bottom = new FormAttachment(100, -53);
		fd_table_1.right = new FormAttachment(100, -31);
		fd_table_1.left = new FormAttachment(quitar, 23);
		table_1.setLayoutData(fd_table_1);
		table_1.setHeaderVisible(true);
		table_1.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(212);
		tblclmnNewColumn_1.setText("Ingrediente");
		
		Label lblIngredientesDelNuevo = new Label(shell, SWT.NONE);
		fd_table_1.top = new FormAttachment(lblIngredientesDelNuevo, 11);
		FormData fd_lblIngredientesDelNuevo = new FormData();
		fd_lblIngredientesDelNuevo.right = new FormAttachment(100, -10);
		lblIngredientesDelNuevo.setLayoutData(fd_lblIngredientesDelNuevo);
		lblIngredientesDelNuevo.setText("INGREDIENTES DEL NUEVO PRODUCTO");
		
		Label lblListaDeIngredientes = new Label(shell, SWT.NONE);
		fd_lblIngredientesDelNuevo.top = new FormAttachment(lblListaDeIngredientes, 0, SWT.TOP);
		fd_table.top = new FormAttachment(lblListaDeIngredientes, 11);
		fd_table.left = new FormAttachment(lblListaDeIngredientes, 0, SWT.LEFT);
		
		for(int i=0;i<ings.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(ings[i].getIngr());
		}
		
		FormData fd_lblListaDeIngredientes = new FormData();
		fd_lblListaDeIngredientes.left = new FormAttachment(0, 10);
		fd_lblListaDeIngredientes.top = new FormAttachment(0, 24);
		lblListaDeIngredientes.setLayoutData(fd_lblListaDeIngredientes);
		lblListaDeIngredientes.setText("LISTA DE INGREDIENTES");
		
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (i==null){
					ArrayList<IngredientVO> ingredientes = new ArrayList<IngredientVO>() ;
					TableItem[] items = table_1.getItems();
					
					for (int ii = 0; ii<items.length; ii ++){
						for (int j=0;j<ings.length;j++){
							if(ings[j]!=null){
							if (items[ii].getText().equalsIgnoreCase(ings[j].getIngr())){
								
						ingredientes.add(ings[j]);
					/*			for(int p=0;p<ings.length;i++){
									if(ings[p].getIngr().equalsIgnoreCase(ings[j].getIngr())){
										ings[p]=null;
									}
								}
							}
								
							}}}*/
					
					i= new IngredientVO[ingredientes.size()];
					for(int p=0;p<ingredientes.size();p++){
						i[p]=ingredientes.get(p);
						System.out.println("p + " + p + ingredientes.get(p).getIngr());
					} 
					
					arts.setIngr(i);
					}}}}}
				
				
				else{
					System.out.println("not null" + i.length);
					ArrayList <IngredientVO> ingredientes = new ArrayList<IngredientVO> ();
					for(int p=0;p<i.length;p++){
						ingredientes.add(i[p]);
					}
					
					
					TableItem[] items = table_1.getItems();
					for (int i = 0; i<items.length; i ++){
						for (int j=0;j<ings.length;j++){
							if (items[i].getText().equalsIgnoreCase(ings[j].getIngr())){
								ingredientes.add(ings[j]);
							
				}}
						}
					IngredientVO [] ip = new IngredientVO[ingredientes.size()];
					for(int p=0;p<ingredientes.size();p++){
						ip[p]=ingredientes.get(p);
						System.out.println("p + " + p + ingredientes.get(p).getIngr());
					} 		
					i=ip;
					arts.setIngr(i);
				}
				
				AddProductos a = new AddProductos(shell, shell.getStyle(),i, arts);
				shell.setVisible(false);
				try {
					a.open();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
			
			}
		});
	
							
		
		FormData fd_btnAceptar = new FormData();
		fd_btnAceptar.bottom = new FormAttachment(100, -10);
		fd_btnAceptar.left = new FormAttachment(0, 236);
		btnAceptar.setLayoutData(fd_btnAceptar);
		btnAceptar.setText("Aceptar");

	}
}
